<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

defined('_JEXEC') or die('Restricted access');

class JobBoardControllerJAUserGroup extends JobBoardController{

    /**
     * Constructor
     */
    function __construct( $default = array() )
    {
        parent::__construct( $default );
        $this->set('name', 'usergroup');
        $this->set('fullname', 'jausergroup');
        $this->registerTask( 'save_resume',        'save' );
        $this->registerTask( 'save_profile',        'save' );
        $this->registerTask( 'cancel_resume',        'cancel' );
        $this->registerTask( 'cancel_profile',        'cancel' );
        
    }
    function display()
    {
    	global $mainframe;
               
        $user = JFactory::getUser();
        if (($user->usertype=='Manager')||($user->usertype=='Administrator')||($user->usertype=='Super Administrator'))
        {
        	$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jajobs', 'layout'=>'jalist'));
        	$msg = JText::_("This function do not support administrator");
        	$this->setRedirect(JRoute::_("index.php?option=com_jajobboard&view=jajobs&layout=jalist&Itemid=$Itemid"),$msg);
        }
        if ($user->id==0)
        {
        	$this->setRedirect(JRoute::_("index.php?option=com_user&view=login"));
        	return ;
        }
        if (getCurrentUserType()=='Employer User'){
        	$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'japrofiles', 'layout'=>'jaview'));
            $link = JRoute::_("index.php?option=com_jajobboard&view=japrofiles&layout=jaview&Itemid=$Itemid");					
            $this->setRedirect($link);
		     
		}
		elseif (getCurrentUserType()=='Job Seeker User')
		{
			$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jaview'));
		    $mainframe->redirect(JRoute::_("index.php?option=com_jajobboard&view=jaresumes&layout=jaview&Itemid=$Itemid"));			   
		}
		else   	parent::display();
    }
    function check()
    {
    	$check = true;
    	$user = &JFactory::getUser();
    	if ($user->user_type!='Job Seeker User')
    	{
    		if (!$this->getResume())
    			$check = false;
    	}
    	else if ($user->user_type!='Employer User')
    	{
    		if (!$this->getProfile())
    			$check = false;    			
    	}
    	
    	return $check;
    }
   
    function save()
    {
    	global $jbconfig;
    	$task = JRequest::getVar('task');
    	$model = & $this->getModel($this->get('fullname'));
    	if ($task == 'save_profile')
    	{
    		require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'japrofiles.php';
			$userdetail = JRequest::getVar('userdetail');
			$user = JFactory::getUser();
			$step = JRequest::getVar('step', ''); 
	        ($step == 'done') or JRequest::checkToken() or jexit( 'Invalid Token' );
	        
	        $Itemid = JRequest::getVar('Itemid');        	        	      
	        $profile_model    = new JobBoardModelJAProfiles();
	        
	        prepare_data('Employer User');
	        $rc = $profile_model->store();
//$rc=1;
	        if($rc)
	        {
	        	if (!JRequest::getVar('edit_profile'))
	        		initial_employer();
	        	if ($jbconfig['account']->get('em_needs_approved')==0)
	        	{
	        		
        	    	$msg = JText::_( 'Successfully updated your profile details').'<br/>'.JText::_('Please use the services of the system.' );
        	    	$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'japrofiles', 'layout'=>'jaview'));   
                	$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=japrofiles&layout=jaview&Itemsid='.$Itemid), $msg );	
	        	}
	        	else 
	        	{
	        		$msg = JText::_( 'Successfully updated your profile details'). '<br/>'.JText::_('Please wait for admin approval before you can perform other activities as an employer' );
	        		$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=jausergroup&save=success&user_type=employer&Itemid=1000'));	
	        	}
	            
	        } else {
	        	
//	            JError::raiseWarning(0, JText::_('THERE ARE SOME INVALID DATA. PLEASE CHECK AGAIN'));
				JRequest::setVar('postback', true);
				Jrequest::setVar('view', 'jausergroup');
				Jrequest::setVar('layout', 'default');
				$post    = JRequest::get('request', JREQUEST_ALLOWHTML);
				JRequest::set($post, 'request');
				parent::display();
	        }    		
    	}
    	elseif ($task == 'save_resume')
    	{
    		require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jaresumes.php';
			$userdetail = JRequest::getVar('userdetail');
			$user = JFactory::getUser();
			$step = JRequest::getVar('step', ''); 
	        ($step == 'done') or JRequest::checkToken() or jexit( 'Invalid Token' );
	        
	        $Itemid = JRequest::getVar('Itemid');        
	        	       
			prepare_data('Job Seeker User');
	        $resume_model    = new JobBoardModelJAResumes();
	        $rc = $resume_model->store();
	        if($rc)
	        {
	        	if (!JRequest::getVar('edit_resume'))
	        		initial_jobseeker();
	        	if ($jbconfig['account']->get('ca_needs_approved')==0){
	        		
	        	    $msg = JText::_( 'Successfully updated your resume details').'<br/>'.JText::_('Please use the services of the system' );
	        	    $Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jaview'));     
	                $this->setRedirect( JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jaview&Itemsid='.$Itemid), $msg );	
	        	}
	            else 
	        	{
	        		
	        		$msg = JText::_( 'Successfully updated your resume details'). '<br/>'.JText::_('Please wait for admin approval before you can perform other activities as a job seeker' );
	        		$this->setRedirect( JRoute::_('index.php?option='.JBCOMNAME.'&view=jausergroup&save=success&user_type=resume&Itemid=1000') );	
	        	}
	        } else {
	        	
//	            JError::raiseWarning(0, JText::_('THERE ARE SOME INVALID DATA. PLEASE CHECK AGAIN'));
				JRequest::setVar('postback', true);
				Jrequest::setVar('view', 'jausergroup');
				Jrequest::setVar('layout', 'default');
				$post    = JRequest::get('request', JREQUEST_ALLOWHTML);
				JRequest::set($post, 'request');
				parent::display();	            
//	            $this->edit(true);
	        }       		
    	}
    	
    }
    
    function cancel()
    {
    	$task = JRequest::getVar('task');
    	$user = JFactory::getUser();
//    	if ($task == 'cancel_profile')
//    	{
//		    if ((getCurrentUserType()=='Guest')&&($user->usertype!='Administrator')&&($user->usertype!='Super Administrator'))
//		    {
//				updateUserType(JBEMPLOYER_USER);
//				$permission_model= new JobBoardModelJAPermissions();
//				$permission_model->initial_per();
//		    }    		
//    	}
//    	else
//    	{
//		    if ((getCurrentUserType()=='Guest')&&($user->usertype!='Administrator')&&($user->usertype!='Super Administrator'))
//		    {
//				updateUserType(JBJOB_SEEKER_USER);
//				$permission_model= new JobBoardModelJAPermissions();
//				$permission_model->initial_per();
//		    }    		
//    	}
    	$msg = JText::_( 'Canceled information updating' );   
	    $this->setRedirect( JRoute::_('index.php?option='. JBCOMNAME .'&view=jajobs'), $msg );
    }
    
    function getResume()
    {
    	$check = true;
    	$check = true;
    	$user = &JFactory::getUser();
    	$db = &JFactory::getDBO();
    	$query = 'SELECT * FROM #__ja_shorlist '
    	.'WHERE (listtype="ja_resumes") AND '
    	.'(user_id="'.$user->id.'")';
    	$db->setQuery($query);
    	$items = $db->loadOjbectList();
    	if (!count($items))
    		$check = false;
    	return $check;
    }
    function getProfile()
    {
    	$check = true;
    	$user = &JFactory::getUser();
    	$db = &JFactory::getDBO();
    	$query = 'SELECT * FROM #__ja_shorlist '
    	.'WHERE (listtype="ja_profiles") AND '
    	.'(user_id="'.$user->id.'")';
    	$db->setQuery($query);
    	$items = $db->loadOjbectList();
    	if (!count($items))
    		$check = false;
    	return $check;
    }
}
?>